![]() |
PATH![]() |
![]() ![]() |
Launch is an application command. If an application is not already running, sending it a Launch command launches it without sending a Run command. (If the application is already running, the Launch command has no effect.) This allows an application to open without performing its usual startup procedures, such as opening a new window or, in the case of a script application, running the script. For example, you can use the Launch command when you don't want a script to cause an application open and close visibly.
Although the target of a Launch command is always an application, the Launch command is actually handled by the Finder. Unlike the other application commands defined in this chapter, it doesn't need to be explicitly supported by applications and doesn't appear in any application's dictionary.
To specify the name ( nameString ) of an application to launch, use a string of the form " Disk : Folder1 : Folder2 :...: ApplicationName " ; for details, see References to Applications. You can also specify a string with only an application name (" ApplicationName "). In this case, AppleScript attempts to find the application using the Desktop Database maintained by the Finder.
AppleScript sends an implicit Run command whenever it begins to execute a Tell statement whose target is an application that is not already open. This can cause problems with applications such as SimpleText that normally perform specific tasks on startup, such as opening a new window. Consider the following example:
tell application "SimpleText"
open file "Hard Disk:Status Report"
end tell
Before AppleScript tells SimpleText to open the file Status Report, it sends SimpleText an implicit Run command. If the application is not already open, the Run command causes SimpleText not only to launch but also to perform its usual startup tasks, including opening an untitled window. Therefore, running this script opens two windows: an untitled window and a window for the file Status Report.
If you don't want AppleScript to send an implicit Run command when it launches an application as the result of a Tell statement, use the Launch command explicitly at the beginning of the statement:
tell application "SimpleText"
launch
open file "Hard Disk:Status Report"
end tell
In this case, AppleScript launches the application without sending it a Run command, and the application opens only a window for the requested document.
The Launch command is particularly useful for scriptable control panels such as the Appearance Manager. For example, if the Appearance Manager isn't already running, either visibly or in the background, the following script will cause it to run and show the Appearance Manager control panel:
tell application "Appearance"
set themeName to name of theme 1
display dialog "The name of the first theme is " & themeName
end tell
However, in the same circumstances, the following script displays the theme information without causing the Appearance Manager to show its user interface (the Appearance Manager control panel). That's because the first line in the Tell block is a launch command, which causes AppleScript to launch the Appearance Manager without sending it a Run command.
tell application "Appearance"
launch
set themeName to name of theme 1
display dialog "The name of the first theme is " & themeName
end tell
For similar reasons, it is sometimes important to use the Launch command before sending the Run command to a script application. For more information, see Calling a Script Application From a Script. For information about Run handlers, see Run Handlers.